package cn.car91.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.car91.R;
import cn.car91.http.HttpHandler;
import cn.car91.utils.RegexUtil;
import cn.car91.view.HerilyAlertDialog;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class RepairShopCommentActivity extends BaseActivity {

    String rid = null;

    @ViewInject(R.id.repairshop_comment_content)
    EditText comment;
    @ViewInject(R.id.repairshop_comment_submit)
    Button submitBtn;
    @ViewInject(R.id.repairshop_comment_attitude)
    RatingBar attitudeBar;
    @ViewInject(R.id.repairshop_comment_charge)
    RatingBar chargeBar;
    @ViewInject(R.id.repairshop_comment_technology)
    RatingBar technologyBar;
    @ViewInject(R.id.repairshop_comment_hardware)
    RatingBar hardwareBar;
    @ViewInject(R.id.repairshop_comment_environment)
    RatingBar environmentBar;
    @ViewInject(R.id.repairshop_comment_good)
    RadioGroup goodGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.activity_repairshop_comment);
        super.onCreate(savedInstanceState);

        String name = getIntent().getStringExtra("rname");
        rid = getIntent().getStringExtra("rid");

        if (RegexUtil.isNotNull(name))
            ((TextView) findViewById(R.id.header_text)).setText(name);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        //login_auto_account.setCursorVisible(false);
        imm.hideSoftInputFromWindow(comment.getWindowToken(), 0);
        return super.onTouchEvent(event);
    }

    @OnClick(R.id.back_btn)
    public void onBackClick(View arg0) {
        finish();
    }

    @OnClick(R.id.repairshop_comment_submit)
    public void onSubmitClick(View arg0) {
        // TODO Auto-generated method stub
        String content = comment.getText().toString();
        if (RegexUtil.isNotNull(content)) {
            if (getCar91Application().getUser() != null && RegexUtil.isNotNull(rid)) {
                RequestParams params = new RequestParams("utf-8");
                params.addBodyParameter("method", "add");
                params.addBodyParameter("type", "repairshop");
                params.addBodyParameter("id", rid);
                params.addBodyParameter("uid", getCar91Application().getUser().getId());

                params.addBodyParameter("content", content);
                float attitude=attitudeBar.getRating() * (100 / attitudeBar.getNumStars());
                float charge=chargeBar.getRating() * (100 / chargeBar.getNumStars());
                float technology=technologyBar.getRating() * (100 / technologyBar.getNumStars());
                float hardware=hardwareBar.getRating() * (100 / hardwareBar.getNumStars());
                float environment=environmentBar.getRating() * (100 / environmentBar.getNumStars());
                String good = "1";
                switch (goodGroup.getCheckedRadioButtonId()) {
                    case R.id.repairshop_comment_goog_g:
                    	good = "0";
                        break;
                    case R.id.repairshop_comment_goog_b:
                    	good = "2";
                        break;
                    default:
                        good = "1";
                        break;
                }
                HttpHandler.httpHandler.addRepairShopComment(rid, getCar91Application().getUser().getId(), content, attitude, charge, technology, hardware, environment, good, requestCallBack);
            }
        } else {
            comment.requestFocus();
        }
    }

    private RequestCallBack requestCallBack = new RequestCallBack() {

        @Override
        public void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            submitBtn.setEnabled(false);
            Drawable drawable = getResources().getDrawable(R.drawable.rotate_progress_circle);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            submitBtn.setCompoundDrawables(null, null, drawable, null);
        }

        @Override
        public void onFailure(HttpException arg0, String arg1) {
            Toast.makeText(RepairShopCommentActivity.this, "网络不给力啊~", Toast.LENGTH_LONG).show();
            submitBtn.setCompoundDrawables(null, null, null, null);
            submitBtn.setEnabled(true);
        }

        @Override
        public void onSuccess(ResponseInfo arg0) {
            if (arg0.result.toString().equals("1")) {
                new HerilyAlertDialog.Builder(RepairShopCommentActivity.this).setMessage("评价成功")
                        .setOnCancelListener(new OnCancelListener() {
                            @Override
                            public void onCancel(DialogInterface arg0) {
                                setResult(RESULT_OK);
                                finish();
                            }
                        }).create().show();
            } else {
                new HerilyAlertDialog.Builder(RepairShopCommentActivity.this).setMessage("发表失败").create().show();
                submitBtn.setCompoundDrawables(null, null, null, null);
                submitBtn.setEnabled(true);
            }
        }
    };
}
